package com.share.thread;

public class TestDeadLock implements Runnable {
	public int flag = 1;
	static Object o1 = new Object();
	static Object o2 = new Object();

	public void run() {
		System.out.println("flag=" + flag);
		if (flag == 1) {
			System.out.println("同步==>o1");
			synchronized (o1) {
				try {
					System.out.println(Thread.currentThread().getName() 
							+ " 111 需要sleep 500毫秒!");
					
					Thread.sleep(500);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
//				synchronized (o2) {
//					System.out.println("1");
//				}
				System.out.println("1");
			}
		}
		if (flag == 0) {
			System.out.println("同步==>o2");
			synchronized (o2) {
				try {
					System.out.println(Thread.currentThread().getName() 
							+ " 000 需要sleep 500毫秒!");
					
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
//				synchronized (o1) {
//					System.out.println("0");
//				}
				System.out.println("0");
			}
		}
	}

	public static void main(String[] args) {
		
		TestDeadLock td1 = new TestDeadLock();
		TestDeadLock td2 = new TestDeadLock();
		
		td1.flag = 1;
		td2.flag = 0;
		
		Thread t1 = new Thread(td1);
		Thread t2 = new Thread(td2);
		
		t1.start();
		t2.start();

	}
}